package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private UserPaidAlbumMapper userPaidAlbumMapper;

	@Operation(summary = "修改当前登录用户基本信息")
	@GuiGuLogin
	@PostMapping("/wxLogin/updateUser")
	public Result updateUser( @RequestHeader("token") String token ,@RequestBody UserInfoVo userInfoVo){

		userInfoService.updateUser(userInfoVo ,token);
		return Result.ok();
	}

	@Operation(summary = "根据用户id查询用户信息")
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	public 	Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfo userInfo = userInfoService.getById(userId);
		UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
		return Result.ok(userInfoVo);

	}

	///api/user/userInfo/userIsPaidTrack/{userId}/{albumId}
	@Operation(summary = "获取用户声音列表付费情况")
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long, Integer> > userIsPaidTrack(@PathVariable Long userId,
													@PathVariable Long albumId,
													@RequestBody List<Long> needChackTrackIdList){
		Map<Long, Integer>  map =userInfoService.userIsPaidTrack(userId,albumId,needChackTrackIdList);
		return Result.ok(map);
	}


	///api/user/userListenProcess/getLatelyTrack
	@GetMapping("/userListenProcess/getLatelyTrack")
	@Operation(summary = "获取用户最近一次播放记录")
	@GuiGuLogin
	public Result<Map<String,Long>> getLatelyTrack(){
		Map<String ,Long> map = userInfoService.getLatelyTrack();
		return Result.ok(map);
	}

	/**
	 * 提供给订单服务调用，验证当前用户是否购买过专辑
	 * @param albumId
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "提供给订单服务调用，验证当前用户是否购买过专辑")
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Long aLong = userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>()
				.eq(UserPaidAlbum::getAlbumId, albumId)
				.eq(UserPaidAlbum::getUserId, userId));
		return Result.ok(aLong>0);

	}

	///api/user/userInfo/findUserPaidTrackList/{albumId}
	@Operation(summary = "根据专辑id+用户ID获取用户已购买声音id列表")
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	@GuiGuLogin
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		List<Long> list = userInfoService.findUserPaidTrackList(albumId,userId);
		return Result.ok(list);

	}

	@GuiGuLogin
	@Operation(summary = "处理用户购买记录（虚拟物品发货）")
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}



}

